package com.example.common.user.view;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.common.R;
import com.example.common.component.MyToast;
import com.example.common.databinding.ActivityRegisterBinding;
import com.example.common.user.viewmodel.RegisterViewModel;


public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "LA";
    ImageView captchaImageView;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityRegisterBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_register);

        //获取ViewModel实例
        RegisterViewModel registerViewModel = new ViewModelProvider(this,
                new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(RegisterViewModel.class);
        //binding数据绑定
        binding.setRegisterViewModel(registerViewModel);
        //设置dataBinding的生命周期，设置后LiveData才会生效
        binding.setLifecycleOwner(this);

        //观测获取验证码状态，获取成功为true
        registerViewModel.getIsGetCaptcha().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    //设置验证码图片
                    if(captchaImageView==null){
                        captchaImageView=findViewById(R.id.register_imageView);
                    }
                    captchaImageView.setImageBitmap(registerViewModel.getBitmap());
                }
            }
        });

        //获取注册状态
        registerViewModel.getIsRegister().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    MyToast.showMyToast(getApplicationContext(),"注册成功\\^O^/",1);
                    //跳转到登录和注册页面
                    ARouter.getInstance().build("/common/user/LoginRegisterActivity").navigation();
                }
            }
        });

        //进度条状态
        registerViewModel.getIsShowProgressBar().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(progressBar==null){
                    progressBar=findViewById(R.id.registerProgressBar);
                }
                if(aBoolean){
                    progressBar.setVisibility(View.VISIBLE);
                }else {
                    progressBar.setVisibility(View.GONE);
                }
            }
        });

        //消息状态
        registerViewModel.getRemainMes().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MyToast.showMyToast(getApplicationContext(),s,registerViewModel.getStatus());
            }
        });
    }
}